package com.hfut.se.zhisms;

import android.content.Intent;
import android.provider.Telephony;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    // These values are temp values.
    private boolean setDefaultFlag;
    private boolean showTipFlag;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("DEBUG", "onCreate");
        setContentView(R.layout.activity_main);

        //Check and remind user the default SMS application
        if (!EnvironmentTools.isDefaultSMSApp(this)) {
            setDefaultSMSApp();
        }
    }


    @Override
    protected void onStart() {
        super.onStart();
        Log.d("DEBUG", "onStart");
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.d("DEBUG", "onResume");
        if (setDefaultFlag) {
            // set default activity is preparing, so do nothing there.
            return;
        }
        if (!EnvironmentTools.isDefaultSMSApp(this)) {
            // App is not default SMS app.
            if (showTipFlag)
                showNotDefaultTip();
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        Log.d("DEBUG", "onPause");
        if (setDefaultFlag) {
            // setting activity is already start, so change flag states.
            setDefaultFlag = false;
            showTipFlag = true;
        }
    }


    @Override
    protected void onStop() {
        super.onStop();
        Log.d("DEBUG", "onStop");
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("DEBUG", "onDestroy");
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    /**
     * This method will start a dialog activity which will ask users to set ZhiSMS to default SMS app.
     *
     * Set {@link #setDefaultFlag} to true.
     */
    private void setDefaultSMSApp() {
        final String PACKAGE_NAME = getPackageName();
        if (!EnvironmentTools.isDefaultSMSApp(this)) {
            // App is not default.
            setDefaultFlag = true;
            Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
            intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, PACKAGE_NAME);
            startActivity(intent);
        }
    }


    /**
     * This method will show a dialog to remind users if this app is not the default SMS app.
     *
     * Clear {@link #setDefaultFlag} to false.
     */
    private void showNotDefaultTip() {
        // clear flag.
        showTipFlag = false;

        if (!EnvironmentTools.isDefaultSMSApp(this)) {
            // App is not default, show a dialog to remind users.
            Intent intent = new Intent(this, DefaultReminder.class);
            intent.putExtra(DefaultReminder.TIP, "可以通过---将智信设置为默认短信软件，以体验更佳功能。");
            startActivity(intent);
        }
    }
}
